// pages/login/login.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
var app=getApp();
Page({
    // 页面的初始数据
    data: {
        phone:null,
        password:null,
        sjlogin:true
    },
//获取手机号
getphone(e)
{
  if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(e.detail.value))) {
    wx.showToast({
    title: '手机号码有误',
    duration: 1000,
    icon:'none'
    });
}
else{
    wx.showToast({
        title: '手机号码正确',
        duration: 1000,
        icon:'none'
        });
    this.data.phone=e.detail.value;
}
},
//获取密码
gopassword(e)
{
    this.data.password=e.detail.value; 
},
//验证登录
gologin()
{
    let that=this;
    wx.request({
        url:app.globalData.url+'/sjlogin', 
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        data: {
            phone:that.data.phone,
            password:that.data.password
         },
         method: 'post',
         success: function (res) {
              if(res.data!="0"&&res.data!="1")
              {
                app.globalData.openid=res.data;
                wx.setStorageSync('openid',res.data);
                wx.showToast({
                  title: '登录成功！',
                  icon:"success",
                  duration:1000
                });
                setTimeout(that.gouser,1000);
              }
              else if(res.data=="1"){
                Notify({ type: 'danger', message: '你尚未注册' });
              }
              else if(res.data=="0")
              {
                Notify({ type: 'danger', message: '密码错误！' });
              }
         },
         fail:function(err){//请求失败之后要做什么
          Notify({ type: 'danger', message: '你尚未注册' });
            }      
    })
},
//跳转到个人
gouser()
{
    wx.switchTab({
      url: '/pages/user/user?openid='+app.globalData.openid,
    })
},
})